﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.BussLogic;
using TimKen.DataEntity;

namespace TimKen.Web
{
    public partial class Message : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindData();
            if (!Page.IsPostBack)
            {
                BindDefaultValue();
            }
        }


        protected string GetReplyContent(string ReplyContent, string ReplyID, string ReplyDate)
        {
            if (ReplyID == "")
                return "";
            else
            {
                return "<div class='custcontent'>" + GetLangString("ReplyContent") + "：<p>" + ReplyContent + "</p></div><div class='r4'>" + GetLangString("ReplyDate") + "： " + ReplyDate + "</div><div class='l6'>" + GetLangString("ReplierID") + "：" + ReplyID + "</div>";
            }
        }

        protected void BindData()
        {
            MessageBoardManager mbm = new MessageBoardManager();
            string where = "   IsDisplay=1 ";

            System.Data.DataSet ds = mbm.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, where);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);
            this.rptMessageList.DataSource = ds.Tables[0].DefaultView;
            this.rptMessageList.DataBind();
        }



        public void BindDefaultValue()
        {
            #region  默认绑定当前用户的一些信息
            if (CurrentUser != null)
            {
                if (CurrentUserType == UserType.SelfRegistered)
                {
                    WebUser user = CurrentUser as WebUser;
                    this.txtUserName.Text = user.UserName;
                    this.txtEmail.Text = user.Email;
                    this.txtCompany.Text = user.Company;
                    this.txtPhone.Text = user.Phone;
                }
                else if (CurrentUserType == UserType.FactoryBuyer)
                {
                    UserAccount user = CurrentUser as UserAccount;
                    this.txtUserName.Text = user.UserName;
                    this.txtEmail.Text = user.Email;
                    this.txtPhone.Text = user.Phone;

                    CustomerManager cm = new CustomerManager();
                    Customer customer = cm.GetModel(user.CustomerID.Value);
                    if (customer != null)
                    {
                        this.txtCompany.Text = customer.CustomerName;
                    }
                }

                this.txtTitle.Text = "";
                this.txtAddress.Text = "";
                this.txtCity.Text = "";
                this.txtState.Text = "";
                this.txtPostalCode.Text = "";
                this.txtFax.Text = "";
                this.txtComments.Text = "";
            }
            #endregion

            else
            {
                this.txtTitle.Text = "";
                this.txtUserName.Text = "";
                this.txtEmail.Text = "";
                this.txtCompany.Text = "";
                this.txtAddress.Text = "";
                this.txtCity.Text = "";
                this.txtState.Text = "";
                this.txtPostalCode.Text = "";
                this.txtPhone.Text = "";
                this.txtFax.Text = "";
                this.txtComments.Text = "";
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            DataEntity.MessageBoard message = new DataEntity.MessageBoard();
            MessageBoardManager mbm = new MessageBoardManager();

            try
            {
                message.Title = this.txtTitle.Text.Trim();
                message.UserName = this.txtUserName.Text.Trim();
                message.Email = this.txtEmail.Text.Trim();
                message.Company = this.txtCompany.Text.Trim();
                message.Address = this.txtAddress.Text.Trim();
                message.City = this.txtCity.Text.Trim();
                message.State = this.txtState.Text.Trim();
                message.PostalCode = this.txtPostalCode.Text.Trim();
                message.Phone = this.txtPhone.Text.Trim();
                message.Fax = this.txtFax.Text.Trim();
                message.Comments = this.txtComments.Text.Trim();
                message.CreateDate = DateTime.Now;
                message.IP = Request.UserHostAddress;
                message.IsDisplay = true;

                mbm.Add(message);

            }
            catch
            {
                Response.Redirect("/Error.aspx");
            }

            BindData();
            BindDefaultValue();
            Alert2(GetLangString("AddSuccessful"), "MessageBoard.aspx");
        }
    }
}